using System;
using System.Collections;
using Microsoft.MediaCenter.UI;


namespace MyVideoViewerMCPlugin
{
    /// <summary>
    /// This object contains the standard set of information displayed in a
    /// content page UI (e.g. Gallery).
    /// </summary>
    public abstract class ContentPage : ModelItem
    {
        private EditableText _JILtext;
        private Int32 _JILindex = new Int32();

        public IntRangedValue Index { get; set; }

        [MarkupVisible]
        internal EditableText JILtext
        {
            get { return _JILtext; }
            set
            {
                if (_JILtext != value)
                {
                    _JILtext = value; base.FirePropertyChanged("JILtext");
                }
            }
        }

        [MarkupVisible]
        internal Int32 JILindex
        {
            get { return _JILindex; }
            set { if (_JILindex != value) { _JILindex = value; base.FirePropertyChanged("JILindex"); } }
        }

        private string _JILDisplayValue;
        [MarkupVisible]
        internal string JILDisplayValue
        {
            get { return _JILDisplayValue; }
            set { if (_JILDisplayValue != value) { _JILDisplayValue = value; base.FirePropertyChanged("JILDisplayValue"); } }
        }


        /// <summary>
        /// The list of items in the page.
        /// This list should only contain objects of type ThumbnailCommand.
        /// </summary>
        public VirtualList Content
        {
            get { return content; }
            set
            {
                if (content != value)
                {
                    content = value;
                    FirePropertyChanged("Content");
                }
            }
        }
        private VirtualList content;

        public ContentPage()
        {
            _JILtext = new EditableText(this.Owner, "JIL");
            JILtext.Value = "";
            JILtext.Submitted += new EventHandler(JILtext_Activity);
            _JILDisplayValue = "";
            this.PropertyChanged += new Microsoft.MediaCenter.UI.PropertyChangedEventHandler(Application_PropertyChanged);
        }

        void Application_PropertyChanged(IPropertyObject sender, string property)
        {
            if (property == "JILDisplayValue" && !string.IsNullOrEmpty(JILDisplayValue))
            {
                int jump = JumpTo(JILDisplayValue);
                if (jump < 0)
                    return;
                jump -= Index.Value;
                if (jump != 0)
                {
                    //if (JILindex == jump)
                    //    JILindex = Index.Value;
                    JILindex = jump;
                }
            }
        }

        void JILtext_Activity(Object sender, EventArgs e)
        {
            JILtext.Value = "";
        }

        public abstract int JumpTo(string displayValue);
    }


    /// <summary>
    /// This object contains the standard set of information displayed in a 
    /// filtered content page UI (e.g. Gallery).
    /// </summary>
    public abstract class FilteredContentPage : ContentPage
    {
        /// <summary>
        /// A choice of available filters on the content.
        /// </summary>
        public virtual Choice Filters
        {
            get { return filters; }
            set
            {
                if (filters != value)
                {
                    // Unhook events on the old value
                    if (filters != null)
                        filters.ChosenChanged -= new EventHandler(OnActiveFilterChanged);

                    filters = value;

                    // Hook up events to the new value
                    if (filters != null)
                        filters.ChosenChanged += new EventHandler(OnActiveFilterChanged);

                    // Fire the "chagned" event
                    OnActiveFilterChanged(filters, EventArgs.Empty);
                    FirePropertyChanged("Filters");
                }
            }
        }

        /// <summary>
        /// Fired when the Chosen value within Filters has been modified
        /// or if the instance of Filters has been changed.
        /// </summary>
        public event EventHandler ActiveFilterChanged;

        /// <summary>
        /// Fire the event for the active filter changing.
        /// </summary>
        protected virtual void OnActiveFilterChanged(object sender, EventArgs args)
        {
            if (ActiveFilterChanged != null)
                ActiveFilterChanged(this, EventArgs.Empty);
        }


        private Choice filters;
    }
}
